package com.zby.scms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zby.scms.entity.Competition;
import com.zby.scms.service.CompetitionService;
import com.zby.scms.utils.BaseResult;
import com.zby.scms.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 竞赛项目competition(Competition)表控制层
 *
 * @author zhangboyi
 * @since 2020-01-14 12:14:28
 */
@Api(tags = "竞赛项目competition(Competition)") 
@RestController
@RequestMapping("competition")
public class CompetitionController {
    /**
     * 服务对象
     */
    @Autowired
    private CompetitionService competitionService;
    private Logger logger = LoggerFactory.getLogger(RoleController.class);

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询 竞赛项目competition")
    @GetMapping("queryById")
    public BaseResult<Competition> queryById(Integer id) {
        Competition competition = competitionService.queryById(id);
        if(competition==null){
            return ResultUtil.error(200, "查询的数据为空");
        }
        return ResultUtil.success(competition);
    }


    @ApiOperation("添加竞赛项目")
    @PostMapping("addCompetition")
    public BaseResult<Competition> addCompetition(Competition Competition){

        return ResultUtil.success(competitionService.addCompetition(Competition));
    }
    /**
     * 通过competetionId删除竞赛项目
     */

    @ApiOperation("删除竞赛项目")
    @DeleteMapping("deleteCompetetion")
    public BaseResult<Competition> deleteCompetetionById(Integer competeId){
        logger.info("删除竞赛项目：");
        Competition currentCompetetion = competitionService.queryById(competeId);
        if(currentCompetetion==null){
            return ResultUtil.error(400, "删除的竞赛项目不存在");
        }
        if(!competitionService.deleteById(competeId)){
            return ResultUtil.error(500,"数据库操作有误");
        }
        logger.info(currentCompetetion.toString());
        return ResultUtil.success("项目删除成功",currentCompetetion);
    }

    /**查询所有项目,查询条件为competetion实体,标签competeLable，竞赛负责人competeUserId
     * 项目名称competetionName、主办方depart、队伍类型teamType
     * 日期查询
    * */

    @ApiOperation("查询竞赛项目")
    @GetMapping("queryAll")
    public BaseResult<List<Competition>> queryAll(Competition competition,Integer pageNum,Integer pageSize){

        if (pageNum == null||pageSize == null){
            logger.info("请求的参数不能为空");
            return ResultUtil.error(400, "请求的参数不能为空");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Competition> list = competitionService.queryAll(competition);
        if (list.size() == 0){
            logger.info("数据为空");
//            return ResultUtil.error(200,"请求成功，但不存在数据");
        }


        PageInfo<Competition> pageInfo = new PageInfo<>(list);

        logger.info("请求成功"+pageInfo.getList().toString());
        return ResultUtil.success(
                pageInfo.getList(),
                pageInfo.getPageNum(),
                pageInfo.getPageSize(),
                pageInfo.getTotal());
    }
}